import 'dart:math';

import 'package:flutter/cupertino.dart';
import 'package:tiktokadult/hj_model/video_model.dart';
import 'package:tiktokadult/hj_page/home_business/home_cell_style/home_section_title.dart';
import 'package:tiktokadult/hj_page/home_business/home_cell_style/video_hor_cell_widget.dart';
import 'package:tiktokadult/hj_page/home_business/home_cell_style/video_simple_cell_widget.dart';
import 'package:tiktokadult/hj_utils/widget_utils.dart';

import '../../../hj_model/home/module_detail_model.dart';

class HomeSectionCell extends StatelessWidget {
  final AllSection section;

  HomeSectionCell(this.section, {super.key});

  @override
  Widget build(BuildContext context) {
    if (section.showType == 0) {
      //一大四小
      return SectionOneBigFourSmall(section);
    } else if (section.showType == 1) {
      //1.5左右滑动(横)
      return SectionOneHalfFiveHor(section);
    } else if (section.showType == 2) {
      //2.5左右滑动(横)
      return SectionTwoHalfFiveHV(section);
    } else if (section.showType == 3) {
      //四宫格
      return SectionGridViewHV(section, maxCount: 4);
    } else if (section.showType == 4) {
      //六宫格
      return SectionGridViewHV(section, maxCount: 6);
    } else if (section.showType == 5) {
      //小列表
      return SectionListView(section);
    } else if (section.showType == 6) {
      //大列表(大小横)
      return SectionListView(section, isBigStyle: true);
    } else if (section.showType == 7) {
      //2.5左右滑动(竖)
      return SectionTwoHalfFiveHV(section, isVertical: true);
    } else if (section.showType == 8) {
      //四宫格(竖)
      return SectionGridViewHV(
        section,
        maxCount: 4,
        aspectRatio: 168 / 254,
        textLines: 1,
      );
    } else if (section.showType == 9) {
      //六宫格(竖)
      return SectionGridViewHV(
        section,
        maxCount: 6,
        aspectRatio: 111 / 148,
        crossAxisCount: 3,
        textLines: 0,
      );
    } else if (section.showType == 10) {
      //九宫格(竖)
      return SectionGridViewHV(
        section,
        maxCount: 9,
        aspectRatio: 111 / 190,
        crossAxisCount: 3,
        textLines: 0,
      );
    } else if (section.showType == 11) {
      //小列表(竖)
      return SectionGridViewSmall(section);
    }
    return SectionOneBigFourSmall(section);
  }
}

//一大四小
class SectionOneBigFourSmall extends StatelessWidget {
  final AllSection section;

  SectionOneBigFourSmall(this.section);

  @override
  Widget build(BuildContext context) {
    if (section.allVideoInfo?.isNotEmpty != true) return SizedBox();
    return Column(
      mainAxisSize: MainAxisSize.min,
      crossAxisAlignment: CrossAxisAlignment.start,
      children: [
        HomeSectionTitle(section),
        Container(
          height: 223,
          padding: EdgeInsets.symmetric(horizontal: 12),
          child: VideoSimpleCellWidget(
            videoModel: section.allVideoInfo!.first,
            textLines: 1,
          ),
        ),
        12.sizeBoxH,
        GridView.builder(
          physics: NeverScrollableScrollPhysics(),
          padding: EdgeInsets.symmetric(horizontal: 12),
          shrinkWrap: true,
          gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
            crossAxisCount: 2,
            mainAxisSpacing: 12,
            crossAxisSpacing: 6,
            childAspectRatio: 168 / 142,
          ),
          itemCount: min(4, section.allVideoInfo!.length - 1),
          itemBuilder: (context, index) {
            VideoModel model = section.allVideoInfo![index + 1];
            return VideoSimpleCellWidget(videoModel: model);
          },
        ),
      ],
    );
  }
}

//1.5横
class SectionOneHalfFiveHor extends StatelessWidget {
  final AllSection section;

  SectionOneHalfFiveHor(this.section);

  @override
  Widget build(BuildContext context) {
    if (section.allVideoInfo?.isNotEmpty != true) return SizedBox();
    return Column(
      mainAxisSize: MainAxisSize.min,
      crossAxisAlignment: CrossAxisAlignment.start,
      children: [
        HomeSectionTitle(section),
        Container(
          height: 190,
          padding: EdgeInsets.only(left: 12),
          child: ListView.builder(
            padding: EdgeInsets.zero,
            itemCount: section.allVideoInfo!.length,
            scrollDirection: Axis.horizontal,
            itemBuilder: (context, index) {
              VideoModel model = section.allVideoInfo![index];
              return Container(
                height: 190,
                width: 280,
                margin: EdgeInsets.only(right: 8),
                child: VideoSimpleCellWidget(videoModel: model, textLines: 1),
              );
            },
          ),
        )
      ],
    );
  }
}

//2.5横/竖
class SectionTwoHalfFiveHV extends StatelessWidget {
  final AllSection section;
  final bool isVertical; //  true 竖, false 横
  SectionTwoHalfFiveHV(this.section, {this.isVertical = false});

  @override
  Widget build(BuildContext context) {
    if (section.allVideoInfo?.isNotEmpty != true) return SizedBox();
    return Column(
      mainAxisSize: MainAxisSize.min,
      crossAxisAlignment: CrossAxisAlignment.start,
      children: [
        HomeSectionTitle(section),
        Container(
          height: isVertical ? 206 : 116,
          padding: EdgeInsets.only(left: 12),
          child: LayoutBuilder(
            builder: (BuildContext context, BoxConstraints constraints) {
              final width = (constraints.maxWidth - 2 * 8) / 2.5;
              return SizedBox(
                height: isVertical ? 218 : 116,
                child: ListView.builder(
                  padding: EdgeInsets.zero,
                  itemCount: section.allVideoInfo!.length,
                  scrollDirection: Axis.horizontal,
                  itemBuilder: (context, index) {
                    VideoModel model = section.allVideoInfo![index];
                    return Container(
                      height: 146,
                      width: width,
                      margin: EdgeInsets.only(right: 8),
                      child: VideoSimpleCellWidget(
                        videoModel: model,
                        textLines: isVertical ? 1 : 2,
                      ),
                    );
                  },
                ),
              );
            },
          ),
        )
      ],
    );
  }
}

//四/六/九宫格横/竖 (默认四宫格)
class SectionGridViewHV extends StatelessWidget {
  final AllSection section;
  final int maxCount;
  final double aspectRatio;
  final int crossAxisCount;
  final int textLines;

  SectionGridViewHV(
    this.section, {
    this.maxCount = 4,
    this.aspectRatio = 168 / 142,
    this.crossAxisCount = 2,
    this.textLines = 2,
  });

  @override
  Widget build(BuildContext context) {
    if (section.allVideoInfo?.isNotEmpty != true) return SizedBox();
    return Column(
      mainAxisSize: MainAxisSize.min,
      crossAxisAlignment: CrossAxisAlignment.start,
      children: [
        HomeSectionTitle(section),
        GridView.builder(
          physics: NeverScrollableScrollPhysics(),
          padding: EdgeInsets.symmetric(horizontal: 12),
          shrinkWrap: true,
          gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
            crossAxisCount: crossAxisCount,
            mainAxisSpacing: 12,
            crossAxisSpacing: 6,
            childAspectRatio: aspectRatio,
          ),
          itemCount: min(maxCount, section.allVideoInfo!.length),
          itemBuilder: (context, index) {
            VideoModel model = section.allVideoInfo![index];
            return VideoSimpleCellWidget(videoModel: model, textLines: textLines);
          },
        ),
      ],
    );
  }
}

//列表横(大小横)
class SectionListView extends StatelessWidget {
  final AllSection section;
  final bool isBigStyle; // true 大横, false 小横
  SectionListView(this.section, {this.isBigStyle = false});

  @override
  Widget build(BuildContext context) {
    if (section.allVideoInfo?.isNotEmpty != true) return SizedBox();
    return Column(
      mainAxisSize: MainAxisSize.min,
      crossAxisAlignment: CrossAxisAlignment.start,
      children: [
        HomeSectionTitle(section),
        ListView.separated(
          physics: NeverScrollableScrollPhysics(),
          padding: EdgeInsets.symmetric(horizontal: 12),
          shrinkWrap: true,
          itemCount: min(3, section.allVideoInfo!.length),
          separatorBuilder: (context, index) => 12.sizeBoxH,
          itemBuilder: (context, index) {
            VideoModel model = section.allVideoInfo![index];
            if (isBigStyle) {
              return SizedBox(
                height: 220,
                child: VideoSimpleCellWidget(videoModel: model, textLines: 1),
              );
            } else {
              return SizedBox(
                height: 96,
                child: VideoHorCellWidget(videoModel: model),
              );
            }
          },
        ),
      ],
    );
  }
}

//小列表(竖)
class SectionGridViewSmall extends StatelessWidget {
  final AllSection section;

  SectionGridViewSmall(this.section);

  @override
  Widget build(BuildContext context) {
    if (section.allVideoInfo?.isNotEmpty != true) return SizedBox();
    return Column(
      mainAxisSize: MainAxisSize.min,
      crossAxisAlignment: CrossAxisAlignment.start,
      children: [
        HomeSectionTitle(section),
        Container(
          padding: EdgeInsets.only(left: 12),
          height: 232,
          child: GridView.builder(
            padding: EdgeInsets.zero,
            scrollDirection: Axis.horizontal,
            gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
              crossAxisCount: 2,
              mainAxisSpacing: 16,
              crossAxisSpacing: 8,
              childAspectRatio: 110 / 256,
            ),
            itemCount: section.allVideoInfo!.length,
            itemBuilder: (context, index) {
              VideoModel model = section.allVideoInfo![index];
              return VideoHorCellWidget(
                videoModel: model,
                imageWidth: 82,
              );
            },
          ),
        ),
      ],
    );
  }
}
